package com.wzy.mall.service.admin.impl;

import com.wzy.mall.mapper.adminMapper.UserMapper;
import com.wzy.mall.model.pojo.adminPOJO.UserPOJO;
import com.wzy.mall.service.admin.UserService;
import com.wzy.mall.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class UserServiceImpl implements UserService {

    public List<UserPOJO> selectAllUsers() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<UserPOJO> userPOJOList = null;
        try {
            userPOJOList = mapper.selectAllUsers();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            sqlSession.close();
        }
        return userPOJOList;
    }

    public int deleteUser(int id) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        int result = 0;
        try {
            result = mapper.deleteById(id);

            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            sqlSession.close();
        }
        return result;
    }

    public List<UserPOJO> selectByNickname(String word) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<UserPOJO> userPOJOList = null;
        try {
            userPOJOList = mapper.selectByNickname(word);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            sqlSession.close();
        }
        return userPOJOList;
    }
}
